home *** CD-ROM | disk | FTP | other *** search
- #include "mpu.hpp"
- #include <conio.h>
- #include <stdlib.h>
- #include <stdio.h>
-
- int dataport = 0x330;
- int statusport = 0x331;
- char uartmode = 0;
-
- MPU::MPU()
- {
- uartmode = 0;
- }
-
- MPU::~MPU()
- {}
-
-
- Soundcard* MPU::recognize()
- {
- char* env = getenv("MPU") ;
-
- if (env)
- {
- int portno;
-
- if (sscanf(env, "%x", &portno) == 1)
- {
- dataport = portno;
- statusport = portno+1;
- if (resetmpu())
- return new MPU;
- }
- }
- dataport = 0x330;
- statusport = 0x331;
- if (resetmpu())
- return new MPU;
- dataport = 0x300;
- statusport = 0x301;
- if (resetmpu())
- return new MPU;
- return 0; // not recognized
- }
-
- int MPU::reset()
- {
- return resetmpu();
- }
-
- int MPU::resetmpu()
- {
- uartmode = 0;
- if (!putmpu(statusport, 0xff))
- return 0;
- return 1;
- }
-
- void MPU::startinput()
- {
- setuart();
- }
-
- void MPU::stopinput()
- {
- reset();
- }
-
- int MPU::getmpu(int port, int aftertimeout)
- {
- register unsigned i;
- for (i = 0xffffU; i; i--)
- if (!isinput())
- break;
- if (i == 0 && !aftertimeout)
- return -1;
- return inp(port);
- }
-
- int MPU::putmpu(int port, unsigned char ch)
- {
- register unsigned i = 0xffff;
- for (i = 0xffffU; i; i--)
- if (!isoutput())
- break;
- if (i == 0 && ch != 0xff)// except for reset command
- return 0; // not allowed
- // send command (ff = reset)
- outp(port, ch);
-
- if (port == statusport) // wait for 0xfe response
- {
- for (i = 0xffffU; i; i--)
- if (!isinput())
- {
- if (inp(dataport) == 0xfe)
- break;
- }
- if (i == 0)
- return 0;
- }
- return 1;
- }
-
- int MPU::getbyte()
- {
- return getmpu(dataport);
- }
-
- int MPU::putbyte(unsigned char c)
- {
- return putmpu(dataport, c);
- }
-
- int MPU::isoutput()
- {
- return inp(statusport) & 0x40;
- }
-
- int MPU::isinput()
- {
- return inp(statusport) & 0x80;
- }
-
- int MPU::hear(unsigned char* buf, int maxlen)
- {
- int n = 0;
- int c;
-
- setuart();
-
- while ( n < maxlen)
- {
- c = getmpu(dataport);
- if (c == -1)
- break;
- *buf++ = c;
- n++;
- }
- return n;
- }
-
- int MPU::play(unsigned char* buf, int len)
- {
- int n = 0;
-
- setuart();
- while (len-- > 0)
- {
- if (!putmpu(dataport, *buf++))
- break;
- n++;
- }
- return n;
- }
-
- int MPU::setuart()
- {
- if (uartmode)
- return 1;
- reset();
- if (!putmpu(statusport, 0x3f))
- return 0;
- uartmode = 1;
- return 1;
- }
-